#!/bin/sh



# This is a script that disables system-wide Snoopy installation.
# You have to run this script as a privileged user.
# This script is also run when you execute "make disable" or
# implicitly when running "make uninstall".



### Where is the library installed?
#
LIBDIR="@SNOOPY_LIBDIR@"



### If unprivileged user is running this script?
#
MY_UID=`id -a | grep -Eo 'uid=[0-9]+' | grep -Eo '[0-9]+'`
if [ "$MY_UID" != "0" ]; then
    echo
    echo 'SNOOPY WARNING: NOT running as privileged user.'
    echo
    exit 0
fi



### Check if writeable /etc/ld.so.preload
#
touch /etc/ld.so.preload
if [ ! -w /etc/ld.so.preload ]; then
    echo
    echo 'SNOOPY ERROR: /etc/ld.so.preload not writable!'
    echo
    exit 1
fi



### Do the actual installation
#
COUNT=`grep -Ec "/(lib)?snoopy.so" /etc/ld.so.preload`
if [ "$COUNT" -gt "1" ]; then
    echo
    echo "SNOOPY ERROR: Multiple instances of (lib)snoopy.so found in /etc/ld.so.preload. Unable to proceed."
    echo
    exit 1
elif [ "$COUNT" -eq "1" ]; then
    echo -n "SNOOPY: Removing from /etc/ld.so.preload: "
    cat /etc/ld.so.preload | grep -E "/(lib)?snoopy.so"
    sed -i "/\/\(lib\)\?snoopy.so/d" /etc/ld.so.preload
    echo "SNOOPY: Disabled."
    echo "SNOOPY: Hint: Your system needs to be restarted to finish Snoopy cleanup."
else
    echo "SNOOPY: Snoopy is NOT enabled on this system."
fi
